package com.genius.unite_platform.goods_order.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.TreeJson;
import com.genius.adminmanager.role.entity.RoleJson;
import com.genius.unite_platform.goods_order.entity.System_info;
import com.genius.unite_platform.goods_order.service.Goods_orderService;
import com.genius.unite_platform.goods_order.util.TreeUtils1;
import com.genius.workflow.log.ControllerLog;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

@Controller
public class Goods_orderSelectController {
	@Autowired
    @Qualifier("goods_orderSelectService")
    private Goods_orderService goods_orderSelectService;
	/**
     * 跳转到列表页面
     * @param session
     * @return
     * @throws Exception
     */
//	@RequiresPermissions("436e683544224a6d9f6d698ccb11f632")
    @GetMapping("/goods_order/toSelect")
    public String toIndex(){
        return "/views/unite_platform/goods_order/select";
    }
	
	/**
     * 跳转到测试页面
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping("/goods_order/index")
    public String getIndex(){
        return "/views/unite_platform/goods_order/index";
    }
    
	 @GetMapping("/goods_order/getSelectHead")
	    @ResponseBody
	    public JSONObject getSelectHead(HttpSession session){
	        JSONObject response = new JSONObject();
	        response.put(ResponseJson.code, 0);
	        JSONObject json = new JSONObject();
	        json.put(MenuJson.menuId, "436e683544224a6d9f6d698ccb11f632");
	        response = goods_orderSelectService.getSelectHead(json, session, response);
	        return response;
	    }
	    
    /**
     * 查询数据
     * @return
     * @throws Exception
     */
    @ControllerLog(description="查看商品订货设置列表数据")
    @PostMapping("/goods_order/getSelectBody")
    @ResponseBody
    public JSONObject getSelectBody(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        response = goods_orderSelectService.getSelectBody(json, response);
        return response;
    }


    @ControllerLog(description="查看商品订货设置表格数据")
    @PostMapping("/goods_order/getSelectSystem")
    @ResponseBody
    public JSONObject getSelectSystem(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        JSONObject search = json.getJSONObject("search");
        if("".equals(search.getString("goodscode"))){
            return response;
        }
        response = goods_orderSelectService.getSelectSystem(json, response);
        return response;
    }


    @PostMapping("/goods_order/getSelectSystemDel")
    @ResponseBody
    public JSONObject getSelectSystemDel(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        response = goods_orderSelectService.getSelectSystemDel(json, response);
        return response;
    }


    @PostMapping("/goods_order/getSelectSystemPriceDel")
    @ResponseBody
    public JSONObject getSelectSystemPriceDel(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        System.out.println(json);
        response = goods_orderSelectService.getSelectSystemPriceDel(json, response);
        return response;
    }


    @PostMapping("/goods_order/getSelectSystemSetDel")
    @ResponseBody
    public JSONObject getSelectSystemSetDel(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        System.out.println(json);
        response = goods_orderSelectService.getSelectSystemSetDel(json, response);
        return response;
    }


    @ControllerLog(description="查看商品价格设置表格数据")
    @PostMapping("/goods_order/getSelectSystemPrice")
    @ResponseBody
    public JSONObject getSelectSystemPrice(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        JSONObject search = json.getJSONObject("search");
        if("".equals(search.getString("goodscode"))){
            return response;
        }
        response = goods_orderSelectService.getSelectSystemPrice(json, response);
        return response;
    }

    @ControllerLog(description="查看陈列协议表格数据")
    @PostMapping("/goods_order/getSelectSystemSet")
    @ResponseBody
    public JSONObject getSelectSystemSet(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        JSONObject search = json.getJSONObject("search");
        if("".equals(search.getString("goodscode"))){
            return response;
        }
        response = goods_orderSelectService.getSelectSystemSet(json, response);
        return response;
    }
    @ControllerLog(description="查看商品订货设置列表数据")
    @PostMapping("/goods_order/getclasslabel")
    @ResponseBody
    public JSONObject getclasslabel(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        response = goods_orderSelectService.getSelectBody(json, response);
        return response;
    }
    @ControllerLog(description="查看商品订货设置列表数据")
    @PostMapping("/goods_order/getclasspurchase")
    @ResponseBody
    public JSONObject getclasspurchase(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        response = goods_orderSelectService.getSelectBody(json, response);
        return response;
    }
    @ControllerLog(description="查看商品订货设置列表数据")
    @PostMapping("/goods_order/classattribute")
    @ResponseBody
    public JSONObject classattribute(@RequestBody JSONObject json){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        response = goods_orderSelectService.getSelectBody(json, response);
        return response;
    }
    @ControllerLog(description="查看系统信息表数据")
	@RequestMapping("/unite_platform/goods_order/roleList")
	@ResponseBody
	public JSONObject roleList(@RequestBody JSONObject json,HttpServletRequest request,HttpSession session){
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		String idValue = null;
        System.out.println(json);

//		for(String str:request.getParameterMap().keySet()){
//			if(!str.equals("{}")){
//				JSONObject pidJson = JSONObject.parseObject(str);
//				idValue = pidJson.getString(TreeJson.pid);
//			}
//		}
        if (!"".equals(json.getString("pid"))){
            idValue = json.getString("pid");
        }
		try {
			String field = System_info.id+","+System_info.name+","+System_info.fatherid+","+System_info.flag+"";
			String table = "system_info";
			JSONArray arr = TreeUtils1.getOnceTree(System_info.id,idValue, field, table, RoleJson.resource,null,null);
			response.put(ResponseJson.status, 1002);
			response.put(ResponseJson.data, arr);
		} catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
		} 
		return response;
	}
    
    @ControllerLog(description="查看系统信息表数据")
	@GetMapping("/unite_platform/goods_order/getsystemInfo")
	@ResponseBody
	public JSONObject getSystemInfo(HttpServletRequest request){
		String systemId = request.getParameter("id");
		String goodscode = request.getParameter("goodscode");
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        response = goods_orderSelectService.getSystemInfo(systemId, goodscode, response);
        return response;
    }
    
}

